package org.xm.sk.vo.tb;

import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.xm.sk.vo.Base;

import com.fasterxml.jackson.annotation.JsonIgnore;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

@Entity
@Getter
@Setter
@Table(name = "dc_organ")
@ApiModel(value = "Organ", parent = Base.class, description = "机构")
public class Organ extends Base {

    /**
     * 
     */
    private static final long serialVersionUID = 4527397127548557844L;

    @ApiModelProperty(value = "机构名称")
    @Column(length = 255)
    private String name;

    @ApiModelProperty(value = "上层机构")
    @JsonIgnore
    @ManyToOne(cascade = { CascadeType.MERGE }, fetch = FetchType.LAZY)
    @JoinColumn(name = "pid")
    private Organ organ;
    
    @ApiModelProperty(value = "上层机构ID")
    @Transient
    private Integer prganId;
    
    @ApiModelProperty(value = "上层机构名称")
    @Transient
    private String prganName;

    @ApiModelProperty(value = "下层机构")
    @JsonIgnore
    @OneToMany(cascade = {
            CascadeType.MERGE }, fetch = FetchType.LAZY, mappedBy = "organ")
    @Fetch(FetchMode.SUBSELECT)
    private Set<Organ> organs = new HashSet<Organ>();

    @ApiModelProperty(value = "机构下的用户")
    @JsonIgnore
    @OneToMany(cascade = {
            CascadeType.MERGE }, fetch = FetchType.LAZY, mappedBy = "organ")
    @Fetch(FetchMode.SUBSELECT)
    private Set<User> users = new HashSet<User>();

    @ApiModelProperty(value = "机构下借调用户")
    @JsonIgnore
    @ManyToMany(cascade = {
            CascadeType.MERGE }, fetch = FetchType.LAZY, mappedBy = "allot_organs")
    @Fetch(FetchMode.SUBSELECT)
    private Set<User> allot_users = new HashSet<User>(0);

}
